package com.senyint.serv;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.senyint.dao.HisDao;
import com.senyint.dao.PacsDao;
import com.senyint.dao.QueueDao;

@Service
@Transactional
public class TestServImpl implements TestServ{
	
	@Autowired
	PacsDao pacsDao;
	
	@Autowired
	HisDao hisDao;
	
	@Autowired
	QueueDao queueDao;
	
	@Transactional(propagation=Propagation.REQUIRED) 
	public void updateTask() {
		try{
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
//	@Transactional(propagation=Propagation.REQUIRED) 
//	public void insertDayEndRecord() {
//		List<Map<String,Object>> list = pacsDao.getNoPayAndRefund();
//		
//		for(int i=0;i<list.size();i++){
//			String recid = UUID.randomUUID().toString().trim().replaceAll("-", "");
//			Map<String,Object> m = list.get(i);
//			m.put("RECID",recid);
//			m.put("DELSTATUS","0");
//			pacsDao.insertDayendRecord(m);
//		}
//	}
	
	public List<Map<String,Object>> selectNoPayAndRefund(Map<String,Object> param) {
		return pacsDao.getNoPayAndRefund(param);
	}
	
	@Transactional(propagation=Propagation.REQUIRED) 
	public void insertPayAndRefund(){
		List<Map<String,Object>> pay = hisDao.selectPay();
		List<Map<String,Object>> refund = hisDao.selectRefund();
		
		pacsDao.deletePay();
		pacsDao.deleteRefund();
		
		for(int i=0;i<pay.size();i++){
			Map<String, Object> m = pay.get(i);
			pacsDao.insertPay(m);
		}
		
		for(int i=0;i<refund.size();i++){
			Map<String, Object> m = refund.get(i);
			pacsDao.insertRefund(m);
		}
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED) 
	public void updateDisableAndDeleteQueue(Map<String,Object> param) {
		try{
			pacsDao.updateDisable(param);
			queueDao.deleteQueue(param);
			String recid = UUID.randomUUID().toString().trim().replaceAll("-", "");
			param.put("RECID",recid);
			param.put("DELTIME", new Date());
			param.put("DELSTATUS", "1");
			param.put("DELLOG", "");
			pacsDao.insertDayendRecord(param);
		}catch(Exception e){
			param.put("DELSTATUS", "2");
			param.put("DELLOG", e.getMessage());
			pacsDao.insertDayendRecord(param);
		}
		
		
	}

}
